package model

import (
	"encoding/json"
	"go.mongodb.org/mongo-driver/bson"
	"testing"
)

func TestCpu_UnmarshallJSON(t *testing.T) {
	TestJsonSuits(
		t,
		[]Suit{
			{&Cpu{FractionOfUsage: 0.25, Unit: cpuUnitCore}, "0.25"},
			{&Cpu{FractionOfUsage: 0, Unit: cpuUnitCore}, "0"},
			{&Cpu{FractionOfUsage: 622.355, Unit: cpuUnitCore}, "622.355"},
		}, func(bytes []byte) (interface{}, error) {
			var reverse Cpu
			err := json.Unmarshal(bytes, &reverse)

			return &reverse, err
		},
	)
}

func TestCpu_UnmarshalBSONValue(t *testing.T) {
	TestBsonSuits(
		t,
		[]Suit{
			{&Cpu{FractionOfUsage: 0.25, Unit: cpuUnitCore}, "{\"$numberDouble\":\"0.25\"}"},
			{&Cpu{FractionOfUsage: 0, Unit: cpuUnitCore}, "{\"$numberDouble\":\"0.0\"}"},
			{&Cpu{FractionOfUsage: 622.3, Unit: cpuUnitCore}, "{\"$numberDouble\":\"622.3\"}"},
		}, func(value bson.RawValue) (interface{}, error) {
			var reverse Cpu
			err := value.Unmarshal(&reverse)

			return &reverse, err
		},
	)
}
